package com.course.finalproject;

import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;

import com.course.finalproject.manager.BatteryManager;
import com.course.finalproject.manager.WeatherManager;

/**
 * Class that represent my global application state.
 * @author	Mariano Bessone <mariano.bessone @ tallertechnologies.com>
 * @since	2012-09-06
 */
public class MyApplication extends Application {

	/** Static instance. */
	private static MyApplication instance;
	
	/** Managers. */
	private WeatherManager weatherManager;
	private BatteryManager batteryManager;
	
	/**
	 * Returns the application instance.
	 * 
	 * @return The application instance.
	 */
	public static MyApplication getInstance() {
		return instance;
	}
	
	/**
	 * Returns the weather manager.
	 * 
	 * @return The weather manager.
	 */
	public WeatherManager getWeatherManager() {
		return weatherManager;
	}
	
	/**
	 * Returns the battery manager.
	 * 
	 * @return The battery manager.
	 */
	public BatteryManager getBatteryManager() {
		return batteryManager;
	}
	
	/**
	 * Executed at the 1st time that the application is launched.
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(Constants.LOG_TAG, "MyApplication.onCreate()");
		instance = this;
		// Create the weather manager and do the first weather request.
		weatherManager = new WeatherManager();
		weatherManager.setWeatherParameters(-31.398484, -64.182086, 6);
		weatherManager.doWeatherRequest();
		// Create the battery manager.
		batteryManager = new BatteryManager();
	}

	/**
	 * Callback to detect any change on the device's configuration, such as a
	 * rotation
	 */
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// Super call and log.
		super.onConfigurationChanged(newConfig);
		Log.d(Constants.LOG_TAG, "MyApplication.onConfigurationChanged()");
	}

	/**
	 * Callback used by the system to allow to the applications to save some
	 * state just a moment before it's destroyed. It's called as soon as Android
	 * detects that is going out of memory.
	 * 
	 * Note that Android does not ensure that this callback will be invoked
	 */
	@Override
	public void onLowMemory() {
		// Super call and log.
		super.onLowMemory();
		Log.d(Constants.LOG_TAG, "MyApplication.onLowMemory()");
	}

	/**
	 * Method called when Android destroys the application. It's just a
	 * convention for the life cycle. Android does not ensure that this callback
	 * will be invoked.
	 */
	@Override
	public void onTerminate() {
		// Super call.
		super.onTerminate();
		// Log.
		Log.d(Constants.LOG_TAG, "MyApplication.onTerminate()");
		// Destructor calls.
		batteryManager.destructor();
		weatherManager.destructor();
	}

}
